<!-- 编辑弹窗 -->
<template>
  <ele-modal
    width="520px"
    :visible="visible"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改角色' : '添加角色'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="92px">
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="角色代码:" prop="code">
             <el-input v-model="form.code"  placeholder="请输入角色代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="角色名称:" prop="name">
            <el-input  v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :md="24" :sm="24">
          <el-form-item label="角色描述:" >
            <el-input type="textarea" :rows="4" placeholder="请输入角色描述" v-model="form.remark" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save" v-if="this.btnindex!='3'">
        保存
      </el-button>
    </div>
  </ele-modal>
</template>

<script>
  import { AddRole,UpdateRole } from '@/api/usermanager/rolemanager';

  const DEFAULT_FORM = {
    id: '',
    name: '',
    code :'',
    remark: '',
    createUser:'',
    createDate:'',
    createUserName:'',
  };

  export default {
    name: 'RoleEdit',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object,
      //按钮索引
      btnindex:String,
    },
    data() {
      return {
        // 表单数据
        form: { ...DEFAULT_FORM },
        // 表单验证规则
        rules: {
          code: [{ required: true, message: '请输入角色代码',trigger: 'blur'}],
          name: [{required: true,message: '请输入角色名称',trigger: 'blur'}],
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
      };
    },

    //初始化数据
    created(){
    },
    methods: {

      /* 保存编辑 */
      save() {
        //校验必填信息
        this.$refs['form'].validate((valid) => {
          if (!valid) {return false;}
          this.loading = true;
          const saveOrUpdate = this.isUpdate ? UpdateRole : AddRole;
          saveOrUpdate(this.form)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              //回调父组件方法
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        //更新父组件中的visible的值
        this.$emit('update:visible', value);
      },
    },
    /* 监听visible属性变化 */
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data) {
            //赋值this.form
            this.$util.assignObject(this.form, {...this.data});
            this.isUpdate = this.isUpdate = this.data.id !=null && this.data.id !=undefined;
          } else {
            this.form.id='';
            this.form.name='';
            this.form.code='';
            this.form.remark='';
            this.form.createUser='';
            this.form.createDate='';
            this.form.createUserName='';
            this.isUpdate = false;
          }
        } else {
          //移除校验结果并重置字段
          // this.$refs['form'].resetFields();
          //移除校验结果
          this.$refs['form'].clearValidate();
          this.form = { ...DEFAULT_FORM };
          if(this.data){
            this.isUpdate = this.isUpdate = this.data.id !=null && this.data.id !=undefined;
          }
        }
      }
    }
  };
</script>
